# coding:utf-8
import redis
import time
from HELLO import HELLO
from zerocm import ZCM
import sys
sys.path.insert(0, './')

# redis 相关
host = sys.argv[1] if len(sys.argv) > 1 else '127.0.0.1'
port = sys.argv[2] if len(sys.argv) > 2 else '6379'
channel = sys.argv[3] if len(sys.argv) > 3 else 'TEST'
rc = redis.StrictRedis(host=host, port=port)
# 订阅消息
ps = rc.pubsub()
ps.subscribe('TEST')
for item in ps.listen():  # 监听状态：有消息发布了就拿过来
    print("*****************")
    print("recv a message")
    if item['type'] == 'message':
        recv_msg = HELLO.decode(item['data'])
        use_time = int(time.time()*1000)-int(recv_msg.time)
        print("recv message: %s, from %s , use time: %s" %
              (recv_msg.str, item['channel'], use_time))
